package org.gbis.openbus.webbean;

import javax.servlet.http.HttpServletRequest;

public class WebBeanRouteStation extends WebBean {

	private String routeId = "";

	private int startSeq = 0;
	private int endSeq = 1000;

	public String getRouteId() {
		return routeId;
	}

	public void setRouteId(String routeId) {
		this.routeId = routeId;
	}

	public int getStartSeq() {
		return startSeq;
	}

	public void setStartSeq(int startSeq) {
		this.startSeq = startSeq;
	}

	public int getEndSeq() {
		return endSeq;
	}

	public void setEndSeq(int endSeq) {
		this.endSeq = endSeq;
	}

	@Override
	public String toString() {
		String format = "WebBeanRouteStation: key:%s, routeId: %s, startSeq: %d, endSeq: %d";
		return String.format(format, this.getKey(), this.getRouteId(), this
				.getStartSeq(), this.getEndSeq());
	}

	@Override
	public Object[] getSearchParams() {
		return new Object[] { this.getRouteId(), this.getStartSeq(), this.getEndSeq() };
	}

	@Override
	public String[] getLogParams() {
		String params[] = new String[10];
		params[0] = "" + this.getRouteId();
		params[1] = "" + this.getStartSeq();
		params[2] = "" + this.getEndSeq();
		return params;
	}

	public void setValuesFrom(HttpServletRequest request) {
		System.out.println("request.getQueryString(): "
				+ request.getQueryString());
		int errorCode = 0;

		WebBeanRouteStation webBeanRouteStation = this;

		webBeanRouteStation.setKey(getString(request, "key"));

		if (errorCode == 0) {
			String str;
			webBeanRouteStation.setRouteId(getString(request, "routeId"));
			str = getString(request, "startSeq");
			if (str != null && str.length() > 0)
				webBeanRouteStation.setStartSeq(toInt(str));
			str = getString(request, "endSeq");
			System.out.println(str);
			if (str != null && str.length() > 0)
				webBeanRouteStation.setEndSeq(toInt(str));
		}
		webBeanRouteStation.setErrorCode(errorCode);

	}

	@Override
	public String getApiName() {
		return "routestation";
	}

	public static WebBeanRouteStation getNewWebBeanRouteStation(
			HttpServletRequest request) {
		WebBeanRouteStation webBeanRouteStation = new WebBeanRouteStation();
		webBeanRouteStation.setValuesFrom(request);
		return webBeanRouteStation;
	}

	/**
	 * This function returns character string to int. If it is null, this
	 * function returns 0.
	 * 
	 * @param str
	 *            the str
	 * @return the int
	 */
	public static int toInt(String str) {
		return toInt(str, 0);
	}

	/**
	 * This function returns character string to int. If it is null, this
	 * function returns defaultValue.
	 * 
	 * @param str
	 *            the str
	 * @param defaultValue
	 *            the default value
	 * @return the int
	 */
	public static int toInt(String str, int defaultValue) {
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

}
